<template>
  <dys-modal
    :open="open"
    :title="t('值集管理')"
    @cancel="emit('cancel')"
    @ok="handleSubmit"
    :confirm-loading="loading"
    :width="1000"
  >
    <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
      <a-row>
        <a-col :span="8">
          <a-form-item
            :label="t('值集名称')"
            v-bind="validateInfos.valueSetName"
          >
            <a-input
              v-model:value="modelRef.valueSetName"
              :placeholder="t('请输入值集名称')"
              allow-clear
            ></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item
            :label="t('值集编码')"
            v-bind="validateInfos.valueSetCode"
          >
            <a-input
              v-model:value="modelRef.valueSetCode"
              :placeholder="t('请输入值集编码')"
              allow-clear
            ></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item
            :label="t('值集状态')"
            v-bind="validateInfos.valueSetStatus"
          >
            <a-switch
              v-model:checked="modelRef.valueSetStatus"
              checked-value="Y"
              un-checked-value="N"
              :checked-children="t('有效')"
              :un-checked-children="t('无效')"
              allow-clear
            ></a-switch>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <edit-table :value-set-id="modelRef.valueSetId" ref="tableRef"></edit-table>
  </dys-modal>
</template>
<script lang="ts" setup>
import { pick } from "lodash";
import { useI18n } from "vue-i18n";
import { reactive, ref, watch } from "vue";
import { Form, message } from "ant-design-vue";
import { add, update } from "@/api/system/value-set";
import type { FormRule } from "@/typing";
import type {
  ValueSetParams,
  QueryResult,
} from "@/api/system/value-set/typing";
import EditTable from "./edit-table.vue";
interface Props {
  /**弹窗 */
  open: boolean;
  /**行数据 */
  model: QueryResult;
}
const props = defineProps<Props>();
const emit = defineEmits(["cancel", "ok"]);
const { t } = useI18n();
const tableRef = ref<InstanceType<typeof EditTable>>();
const initValue = (): ValueSetParams => ({
  valueSetId: null,
  valueSetName: null,
  valueSetCode: null,
  valueSetStatus: "Y",
});
const modelRef = reactive(initValue());
const modelRule = reactive<FormRule<ValueSetParams>>({
  valueSetName: [
    {
      required: true,
      message: () => t("请输入值集名称"),
    },
  ],
  valueSetCode: [{ required: true, message: () => t("请输入值集编码") }],
  valueSetStatus: [
    { required: true, message: () => t("Please select the value set status") },
  ],
});
const { validate, validateInfos, resetFields } = Form.useForm(
  modelRef,
  modelRule
);
watch([() => props.model, () => props.open], ([model]) => {
  resetFields(pick(model, Object.keys(modelRef)) || initValue());
});
const loading = ref(false);
/**提交 */
const handleSubmit = async () => {
  try {
    await validate();
  } catch {
    return message.warning(t("校验失败"));
  }
  if (tableRef.value?.editRecord) {
    return message.warning(t("请先保存正在编辑的行"));
  }
  loading.value = true;
  const { success } = await (props.model?.valueSetId ? update : add)(modelRef);
  loading.value = false;
  if (success) {
    message.success(props.model?.valueSetId ? t("修改成功") : t("新增成功"));
    emit("ok");
    emit("cancel");
  }
};
</script>
